<!--
 * @Des: 
 * @Author: jin_yc
 * @Date: 2019-12-06 16:40:52
 * @LastEditor: jin_yc
 * @LastEditTime: 2021-12-09 14:40:45
 -->
<template>
  <fks-dialog @close="close" width="480px" :visible="visible">
    <div slot="title">
      <i class="fks-icon-edit" :style="{ color: theme }"></i>
      {{ LANG.TASKDELEGATE }}
    </div>
    <fks-form
      :model="dataInfo"
      :rules="rules"
      ref="delegateForm"
      label-width="85px"
    >
      <fks-form-item :label="LANG.TASKSUBJECT" prop="taskSubject">
        <fks-input readonly v-model="taskInfo.taskSubject"></fks-input>
      </fks-form-item>
      <fks-form-item :label="LANG.TASKNAME" prop="taskName">
        <fks-input readonly v-model="taskInfo.taskName"></fks-input>
      </fks-form-item>
      <fks-form-item :label="LANG.EXECUTOR" prop="taskAsigneeName">
        <fks-user-selector
          readonly
          :username.sync="taskInfo.taskAsignee"
          :userfullname.sync="taskInfo.taskAsigneeName"
        ></fks-user-selector>
      </fks-form-item>
      <fks-form-item :label="LANG.DELEGATOR" prop="userName">
        <fks-user-selector
          style="width: 100%"
          url
          :username.sync="dataInfo.userName"
          :userfullname.sync="dataInfo.userFullName"
        ></fks-user-selector>
      </fks-form-item>
      <fks-form-item :label="LANG.DELEGATEREASON" prop="comment">
        <fks-input
          type="textarea"
          v-model="dataInfo.comment"
          :maxlength="150"
        ></fks-input>
      </fks-form-item>
    </fks-form>
    <div slot="footer">
      <!-- <fks-button @click="close" plain type="primary">{{LANG.CANCEL}}</fks-button> -->
      <fks-button
        type="primary"
        icon="fks-icon-check"
        @click="submitDelegate"
        >{{ LANG.SUBMIT }}</fks-button
      >
    </div>
  </fks-dialog>
</template>

<script>
import Mix from '@/mixins/module'
import * as types from '@/store/Getter/getterTypes.js'
import { taskDelegate } from './api'
export default {
  name: 'delegate',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    taskInfo: {
      type: Object,
      default() {
        return {
          taskId: '',
          taskName: '',
          taskSubject: '',
          taskAsigneeName: ''
        }
      }
    }
  },
  mixins: [Mix],
  data() {
    return {
      dataInfo: {
        userName: '',
        userFullName: '',
        comment: ''
      }
    }
  },
  computed: {
    theme() {
      return this.$store.getters[types.THEME]
    },
    rules() {
      return {
        userName: [
          {
            required: true,
            message: this.LANG.VALIDATE,
            trigger: 'change'
          },
          {
            trigger: 'change',
            validator: (rule, value, callack) => {
              if (value === this.taskInfo.taskAsignee) {
                callack(new Error('原执行人不能和新执行人相同'))
              } else {
                callack()
              }
            }
          }
        ],
        comment: [
          { required: true, message: this.LANG.VALIDATE, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitDelegate() {
      console.log(222)
      this.$refs.delegateForm.validate((valid) => {
        if (valid) {
          console.log(valid)
          taskDelegate(this.taskInfo.taskId, {
            comment: this.dataInfo.comment,
            userName: this.dataInfo.userName
          }).then((res) => {
            if (res.status) {
              if (this.$parent.getData) {
                this.$parent.getData()
              }
              this.$message({
                type: 'success',
                message: this.LANG.DELEGATE_SUCCESS,
                showClose: true
              })
            }
          })
          this.close()
        } else {
          this.$message({
            type: 'error',
            message: '请完善表单',
            showClose: true
          })
        }
      })
    },
    close() {
      this.dataInfo.userName = ''
      this.dataInfo.userFullName = ''
      this.dataInfo.comment = ''
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style></style>
